package com.borqsprocessmanager.info;

import java.util.ArrayList;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.R;
import com.borqsprocessmanager.util.Log;

import android.content.pm.ComponentInfo;
import android.os.Parcel;
import android.os.Parcelable;

public class ReceiverProcessInfo extends SectionInfo implements
        Comparable<ReceiverProcessInfo>, Parcelable {

    public String head_scheduled_running;
    public String head_more;
    public String head_cpu_setting;

    public static final int AUTOP_ROCESS = 9;
    public static final int COMMON_PROCESS = 8;
    public static final int CPU_SETTING = 30;

    public static final int CLOSE = 2;
    public static final int OPEN = 1;

    public ArrayList<String> reivevers = new ArrayList<String>();
    public String appName;
    public String processName;
    public String packageName;
    public int type;
    public int isClose;
    public int uid;
    public int[] pid;

    public ReceiverProcessInfo() {
        head_scheduled_running = App.mContext
                .getString(R.string.sheduled_running);
        head_more = App.mContext.getString(R.string.more);
        head_cpu_setting = App.mContext.getString(R.string.cpu);
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getProcessName() {
        return processName;
    }

    public void setProcessName(String processName) {
        this.processName = processName;
    }

    public void setReivever(String reivever) {
        this.reivevers.add(reivever);
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    /**
     * May be one of {@link #COMPONENT_ENABLED_STATE_ENABLED},
     * {@link #COMPONENT_ENABLED_STATE_DISABLED}, or
     * {@link #COMPONENT_ENABLED_STATE_DEFAULT}. The last one means the
     * component's enabled state is based on the original information in the
     * manifest as found in {@link ComponentInfo}.
     * 
     * @param isClose
     */
    public int getIsClose() {
        return isClose;
    }

    /**
     * May be one of {@link #COMPONENT_ENABLED_STATE_ENABLED},
     * {@link #COMPONENT_ENABLED_STATE_DISABLED}, or
     * {@link #COMPONENT_ENABLED_STATE_DEFAULT}. The last one means the
     * component's enabled state is based on the original information in the
     * manifest as found in {@link ComponentInfo}.
     * 
     * @param isClose
     */
    public void setIsClose(int isClose) {
        this.isClose = isClose;
    }

    public int[] getPid() {
        return pid;
    }

    public void setPid(int[] pid) {
        this.pid = pid;
    }

    @Override
    public int compareTo(ReceiverProcessInfo another) {
        // if ((this.isClose == 2) == (another.isClose == 2)) {
        //
        // return appName.compareTo(another.appName);
        // }
        // if (this.isClose == 2) {
        // return -1;
        // }
        //
        // if (this.isClose == 1 && another.isClose == 2) {
        // return 1;
        // }
        //
        // if (this.isClose == 1 && another.isClose == 1) {
        // return appName.compareTo(another.appName);
        // }

        if (this.type == ReceiverProcessInfo.AUTOP_ROCESS) {
            if (another.type == ReceiverProcessInfo.AUTOP_ROCESS) {
                if ((this.isClose > another.isClose)) {
                    return -1;
                } else {
                    return 0;
                }
            }
            return -1;
        }

        if (this.type == ReceiverProcessInfo.COMMON_PROCESS
                && another.type == ReceiverProcessInfo.COMMON_PROCESS) {
            if ((this.isClose > another.isClose)) {
                return -1;
            }
            if ((this.isClose == another.isClose)) {
                return appName.compareTo(another.appName);
            }
            return 1;
        }
        return 1;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    /**
     * 
     * public ArrayList<String> reivevers = new ArrayList<String>(); public
     * String appName; public String processName; public String packageName;
     * public int type; public int isClose; public int uid; public int[] pid;
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(reivevers);
        dest.writeString(appName);
        dest.writeString(packageName);
        dest.writeInt(type);
        dest.writeInt(isClose);
        dest.writeInt(uid);
        dest.writeIntArray(pid);
    }

    public static final Parcelable.Creator<ReceiverProcessInfo> CREATOR = new Parcelable.Creator<ReceiverProcessInfo>() {

        public ReceiverProcessInfo createFromParcel(Parcel in) {
            return new ReceiverProcessInfo(in);
        }

        public ReceiverProcessInfo[] newArray(int size) {
            return new ReceiverProcessInfo[size];
        }
    };

    private ReceiverProcessInfo(Parcel in) {
        reivevers = in.readArrayList(String.class.getClassLoader());
        Log.v("xxx", "reivevers===" + reivevers.size());
        appName = in.readString();
        packageName = in.readString();
        type = in.readInt();
        isClose = in.readInt();
        uid = in.readInt();
        // in.readIntArray(pid);
        pid = in.createIntArray();
    }
}
